package com.lion.common.modules.uaa.web.controller;

import com.chris.dev.base.libs.v1.http.params.NoneParam;
import com.chris.dev.base.libs.v1.http.results.ValueResult;
import com.lion.common.base.exceptions.CommonException;
import com.lion.common.modules.uaa.service.def.AccountService;
import com.lion.common.modules.uaa.service.def.AuthAccountService;
import com.lion.common.modules.uaa.web.api.AccountApi;
import com.lion.common.modules.uaa.web.models.params.CompleteAccountParam;
import com.lion.common.modules.uaa.web.models.params.UpdateAccountRoleParam;
import com.lion.common.modules.uaa.web.models.params.UpdatePasswordByEmailVerifyCodeParam;
import com.lion.common.modules.uaa.web.models.params.UpdatePasswordByPhoneVerifyCodeParam;
import com.lion.common.modules.uaa.web.models.results.LoginResult;
import com.lion.common.modules.uaa.web.models.results.UserInfoResult;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Chris Chan
 * Create On 2025/7/18 下午11:13
 * Use for: 账号相关接口
 * Explain:
 * 需要授权
 */
@Slf4j
@RestController
@RequestMapping("account")
public class AccountController implements AccountApi {
    AccountService accountService;
    AuthAccountService authAccountService;

    @Autowired
    public AccountController(AccountService accountService, AuthAccountService authAccountService) {
        this.accountService = accountService;
        this.authAccountService = authAccountService;
    }

    @Override
    public ValueResult<Boolean> updatePasswordByPhoneVerifyCode(@RequestBody @Valid UpdatePasswordByPhoneVerifyCodeParam param) {
        return ValueResult.of(accountService.updatePasswordByMobileVerifyCode(param.getPhone(),
                param.getVerifyCode(), param.getNewPassword(), param.getConfirmPassword()));
    }

    @Override
    public ValueResult<Boolean> updatePasswordByEmailVerifyCode(@RequestBody @Valid UpdatePasswordByEmailVerifyCodeParam param) {
        return ValueResult.of(accountService.updatePasswordByEmailVerifyCode(param.getEmail(),
                param.getVerifyCode(), param.getNewPassword(), param.getConfirmPassword()));
    }

    @Override
    public LoginResult refreshLoginResult(@RequestBody NoneParam param, @RequestHeader("Authorization") String authorization) {
        // 取出token
        if (!StringUtils.hasText(authorization)) {
            throw CommonException.of("authorization不能为空");
        }
        log.info("authorization:{}", authorization);
        return authAccountService.refreshLoginResult(authorization);
    }

    @Override
    public ValueResult<Boolean> logout(@RequestBody NoneParam param) {
        return ValueResult.of(accountService.logout());
    }

    @Override
    public ValueResult<Boolean> completeAccount(@RequestBody @Valid CompleteAccountParam param) {
        return ValueResult.of(accountService.completeAccount(param));
    }

    @Override
    public ValueResult<Boolean> updateAccountRole(@RequestBody @Valid UpdateAccountRoleParam param) {
        return ValueResult.of(accountService.updateAccountRole(param.getAccountCode(), param.getRoleCodes()));
    }

    @Override
    public UserInfoResult getUserInfo() {
        return accountService.getUserInfo();
    }
}
